import facade
from xjlibrary.our_file_dir import BaseDir

curpath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curpath, -2)
dirPath = BaseDir.get_new_path(TopPath, "download", "yuetong_bz", "download", "listhtml")
BaseDir.create_dir(dirPath)


class DownList(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.url = "http://zt.yuetong.cn/rmjt/fg/searchResult.html?name=&"
        self.headers = {
            "Accept": "text/html, */*; q=0.01",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Origin": "http://zt.yuetong.cn",
            "Referer": "http://zt.yuetong.cn/rmjt/fg/search.html?title=&objectType=1&classifyCode=&contentType=undefined",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
            "X-Requested-With": "XMLHttpRequest"
        }

    def down_page(self):
        postdate = {
            "status": "2",
            "classifyCode": "",
            "num": "1",  # 该参数为页数
            "author": "",
            "keyword": "",
            "key": "",
            "contentType": "4"
        }
        # 由于获取总页码比较麻烦，所以页码已被写定，如果后面页码有所变化，
        # 请修改页码
        for i in range(1, 69):
            filepath = BaseDir.get_new_path(dirPath, str(i) + ".html")
            if BaseDir.is_file_exists(filepath):
                self.logger.info(filepath + "文件存在")
                continue
            postdate["num"] = str(i)
            BoolResult, errString, r = facade.BaseRequestPost(self.url,
                                                              endstring="",
                                                              mark="minboxlists",
                                                              data=postdate,
                                                              headers=self.headers,
                                                              timeout=(30, 60)
                                                              )
            if BoolResult:
                BaseDir.single_write_file(r.text, BaseDir.get_new_path(dirPath, str(i) + ".html"))
            else:
                self.logger.info("下载出错")


if __name__ == "__main__":
    down = DownList()
    down.down_page()
